const db = uniCloud.database();

async function addHmsToken(token, desc) {
  let result = await db.collection("config").add({
    type: 'hmsToken',
    token,
    desc
  })
  return result
}

async function removeHmsToken(id) {
  let result = await db.collection("config").doc(id).remove()
  return result
}

async function updateHmsToken(id, token, desc) {
  token = token ? token : undefined
  desc = desc ? desc : undefined
  let result = await db.collection("config").doc(id).update({
    type: 'hmsToken',
    token: token,
    desc: desc,
  })
  return result
}

async function getHmsTokenList() {
  let result = await db.collection("config").where({type: 'hmsToken'}).get()
  return result
}

async function getHmsMessageList() {
  let result = await db.collection("hms-list").orderBy("sendTimeId", "desc").limit(10).get()
  return result
}

async function removeHmsMessage(id) {
  let result = await db.collection("hms-list").doc(id).remove()
  return result
}

module.exports = {
  addHmsToken,
  removeHmsToken,
  updateHmsToken,
  getHmsTokenList,
  getHmsMessageList,
  removeHmsMessage,
};
